using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Reflection;

namespace SPALM.SharePointSoftwareFactory.SetupHelper
{
    public partial class ComponentContainer : UserControl
    {
        public ComponentContainer()
        {
            InitializeComponent();

            string downloadDir = Path.Combine(Path.GetTempPath(), "DownloadSPSF");
            if (!Directory.Exists(downloadDir))
            {
                Directory.CreateDirectory(downloadDir);
            }

            //# For Visual Studio 2005: HKEY_CLASSES_ROOT\VisualStudio.DTE.8.0
            //# For Visual Studio 2008: HKEY_CLASSES_ROOT\VisualStudio.DTE.9.0
            Stream resourceStream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("SPALM.SharePointSoftwareFactory.SetupHelper.Components2008.xml");

            if (resourceStream != null)
            {
                this.LoadControls(downloadDir, resourceStream);
            }

            //load list of authors
        }

        public void LoadControls(string downloadDir, Stream filename)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filename);

            ComponentHeader header = new ComponentHeader("Required Components");
            header.Dock = DockStyle.Top;
            panelMain.Controls.Add(header);
            header.BringToFront();

            foreach (XmlNode node in doc.SelectNodes("/Components/RequiredComponents/RequiredComponent"))
            {
                ComponentDownload cd = new ComponentDownload(this, downloadDir, node);

                cd.Dock = DockStyle.Top;
                panelMain.Controls.Add(cd);
                cd.BringToFront();
            }

            ComponentHeader header2 = new ComponentHeader("Recommmended Components");
            header2.Dock = DockStyle.Top;
            panelMain.Controls.Add(header2);
            header2.BringToFront();

            foreach (XmlNode node in doc.SelectNodes("/Components/OptionalComponents/OptionalComponent"))
            {
                ComponentDownload cd = new ComponentDownload(this, downloadDir, node);
                cd.Dock = DockStyle.Top;
                panelMain.Controls.Add(cd);
                cd.BringToFront();
            }
        }

        public void DownloadingInProgress(ComponentDownload component)
        {
            foreach (Control c in panelMain.Controls)
            {
                if (c is ComponentDownload)
                {
                    if (c != component)
                    {
                        c.Enabled = false;
                    }
                }
            }
        }

        internal void DownloadingFinished(ComponentDownload componentDownload)
        {
            foreach (Control c in panelMain.Controls)
            {
                if (c is ComponentDownload)
                {
                    c.Enabled = true;
                }
            }
        }

        public void CheckInstallation()
        {
            foreach (Control c in panelMain.Controls)
            {
                if (c is ComponentDownload)
                {
                    ((ComponentDownload)c).CheckInstallation();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckInstallation();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CheckInstallation();
        }

        internal void SetVSVersions(bool vs2008, bool vs2010)
        {
            foreach (Control control in panelMain.Controls)
            {
                if (control is ComponentDownload)
                {
                    (control as ComponentDownload).SetVSVersions(vs2008, vs2010);
                }
            }
        }
    }
}
